package com.ijiuchuang.jc_localization.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.FrameLayout;

import com.ijiuchuang.jc_localization.R;
import com.ijiuchuang.jc_localization.view.fragment.AboutFragment;
import com.ijiuchuang.jc_localization.view.fragment.DeviceListFragment;
import com.ijiuchuang.jc_localization.view.fragment.OpenSourceFragment;
import com.ijiuchuang.jc_localization.view.fragment.SettingsFragment;
import com.ijiuchuang.jc_localization.view.fragment.SettsLocationFragment;
import com.ijiuchuang.jc_localization.view.fragment.SettsMapDetailFragment;
import com.ijiuchuang.jc_localization.view.fragment.SettsMapListFragment;
import com.ijiuchuang.jc_localization.view.fragment.ToolsFragment;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FragmentActivity extends AppCompatActivity {

    @BindView(R.id.fragment_toolbar)
    Toolbar toolbar;
    @BindView(R.id.fragment_frame_content)
    FrameLayout frameContent;

    public static final String FRAGMENT_NAME = "FragmentActivity.FRAGMENT_NAME";
    public static final int FRAGMENT_NAME_SETTINGS = 0;
    public static final int FRAGMENT_NAME_DEVICE_LIST = 1;
    public static final int FRAGMENT_NAME_ABOUT = 2;
    public static final int FRAGMENT_NAME_OPEN_SOURCE = 3;
    public static final int FRAGMENT_NAME_TOOLS = 4;
    public static final int FRAGMENT_NAME_SETTS_LOCATION = 5;
    public static final int FRAGMENT_NAME_SETTS_MAP_LIST = 6;
    public static final int FRAGMENT_NAME_SETTS_MAP_DETAIL = 7;

    private String title[] = new String[]{"设置", "设备列表", "关于", "开源许可", "工具", "定位设置", "地图列表", "地图设置"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);
        ButterKnife.bind(this);

        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentActivity.this.finish();
            }
        });

        Intent intent = getIntent();
        int type = intent.getIntExtra(FRAGMENT_NAME, -1);
        setTitle(title[type]);

        setDefaultFragment(type);
    }

    private void setDefaultFragment(int type)
    {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.fragment_frame_content, getFragment(type));
        transaction.commit();
    }

    private Fragment getFragment(int position) {
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(FRAGMENT_NAME_SETTINGS, new SettingsFragment());
        fragments.add(FRAGMENT_NAME_DEVICE_LIST, new DeviceListFragment());
        fragments.add(FRAGMENT_NAME_ABOUT, new AboutFragment());
        fragments.add(FRAGMENT_NAME_OPEN_SOURCE, new OpenSourceFragment());
        fragments.add(FRAGMENT_NAME_TOOLS, new ToolsFragment());
        fragments.add(FRAGMENT_NAME_SETTS_LOCATION, new SettsLocationFragment());
        fragments.add(FRAGMENT_NAME_SETTS_MAP_LIST, new SettsMapListFragment());
        fragments.add(FRAGMENT_NAME_SETTS_MAP_DETAIL, new SettsMapDetailFragment());

        return fragments.get(position);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        finish();
    }

}
